var Api = require("../../common/api/api.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 授权登录
   */
  authorLogin: function (e) {
    let _this = this;
    if (e.detail.errMsg !== 'getUserInfo:ok') {
      return false;
    }
    wx.showLoading({ title: "正在登录", mask: true });
    // 执行微信登录
    wx.login({
      success: function (res) {
        console.log(e.detail.rawData);
        
        // 将json字符串转 json对象
        var jsonStr = e.detail.rawData;
        if(typeof jsonStr!= 'object'){
          jsonStr= jsonStr.replace(/\ufeff/g,"");//重点
          jsonStr = JSON.parse(jsonStr);
        }

        //请求参数封装
        var data = {
          code: res.code,
          userInfo: jsonStr,
        }
        var params = JSON.stringify(data)

        //登录
        Api.signIn(params).then(function (result) {
          console.log("登录返回数据：", result.data.data)
          wx.setStorageSync('openid', result.data.data.openid);
          wx.setStorageSync('resUrl', result.data.data.resUrl);
          wx.setStorageSync('userId', result.data.data.userId);
          wx.hideLoading();
          wx.switchTab({
            url: '../demandhall/demandhall',
          });
        }).catch(function (error) {
          console.log(error)
        })
      }
    });
  },


})